package angels.client.Activities;

import com.readystatesoftware.mapviewballoons.R;

import android.R.color;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import angels.client.Entities.Categories;
import angels.client.Entities.Categories.Type;
import angels.client.Listeners.MainMenuListener;
import angels.client.Services.BusyHourService;

public class MainMenu extends Activity implements MainMenuListener,ServiceConnection{

    TextView title;
    TextView body;
    ImageView image;
    LinearLayout litLay;
    private BusyHourService service = null;
    
    public void onServiceConnected(ComponentName className, IBinder binder) {
		service = ((BusyHourService.LocalBinder) binder).getService();
	}

	public void onServiceDisconnected(ComponentName className) {
	    service = null;
	}

    
    @Override	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Typeface typeLacllig = Typeface.createFromAsset(getAssets(),"fonts/Righteous-Regular.ttf");
//        /Typeface typeAriel = Typeface.createFromAsset(getAssets(),"fonts/ariel.ttf"); 
        
        image=new ImageView(this);
        title = new TextView(this);
        title.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        title.setGravity(Gravity.CENTER_HORIZONTAL);
        title.setClickable(false);
        title.setFocusable(false);
        title.setTextSize(46);
        title.setText("Angels");
        title.setTextColor(0xFFFFFFFF);
        title.setTypeface(typeLacllig);
        
        body = new TextView(this);
        body.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        body.setGravity(Gravity.CENTER_HORIZONTAL);
        body.setPadding(0, 10, 0, 10);
        //title.setTypeface(typeAriel);
        body.setClickable(false);
        body.setFocusable(false);
        
        EditText txtUsername=new EditText(this);
        Button btnLogin = new Button(this);
        btnLogin.setText("Login");
        btnLogin.setBackgroundResource(R.drawable.custom_button_brown);
        
        btnLogin.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	Toast.makeText(getApplicationContext(), "Loading...", Toast.LENGTH_SHORT).show();
            	service.getCategories(MainMenu.this,Type.category,null);
            }
        });
        
       
        LinearLayout layout = new LinearLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackgroundColor(0xFF69A74E);
        layout.setPadding(20, 20, 20, 20);
        
        layout.addView(title);
        layout.addView(image);
        layout.addView(body);
        layout.addView(txtUsername);
        layout.addView(btnLogin);
        setContentView(layout);
        
        body.setText("Enter your Angels username:");
        body.setTextColor(0xFFFFFFFF);
        image.setImageResource(R.drawable.icon);
        
        Intent ServiceGps = new Intent(this, BusyHourService.class);
        getApplicationContext().bindService(ServiceGps, this, Context.BIND_AUTO_CREATE);
    }

	@Override
	public void onCategoriesReceived(Categories categories) {
		Intent intent= new Intent(getApplicationContext(), CategoryMenu.class);
		intent.putExtra("categories", categories.getCategories());
	  	startActivity(intent);			
	}
	
	public void onCategoriesReceivedError()
	{
		//TODO: Read history from file or show a clean map
		Intent intent = new Intent(getApplicationContext(), TrafficActivity.class);
   	 	intent.putExtra("category", "");
   	 	startActivity(intent);
	}
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu){
    	startActivity(new Intent(this, SettingsActivity.class));
    	return true;
    }
}